﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GuessMapArranger
{
    class Program
    {
        static String srcFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "src");
        static String tempFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "temp");
        static String distFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "dist");
        const int chapterCount = 50;
        const int startChapter = 4;
        const int startStage = 1;


        static void Main(string[] args)
        {
            DirectoryInfo dirInfo = new DirectoryInfo(srcFolder);

            if (!Directory.Exists(tempFolder))
            {
                Directory.CreateDirectory(tempFolder);
            }
            else
            {
                Directory.Delete(tempFolder, true);
                Directory.CreateDirectory(tempFolder);
            }

            if (!Directory.Exists(distFolder))
            {
                Directory.CreateDirectory(distFolder);
            }
            else
            {
                Directory.Delete(distFolder, true);
                Directory.CreateDirectory(distFolder);
            }

            CopySrcToTemp(dirInfo);

            CopyToDist();
        }

        private static void CopyToDist()
        {
            DirectoryInfo tempDirInfo = new DirectoryInfo(tempFolder);

            int chapter = startChapter;
            int stageIndex = startStage;


            foreach (var fileInfo in tempDirInfo.GetFiles("*.map"))
            {
                String fileName = "stage" + chapter + "_" + stageIndex + ".map";
                File.Copy(fileInfo.FullName, Path.Combine(distFolder, fileName));

                stageIndex++;
                if (stageIndex > chapterCount)
                {
                    stageIndex = 1;
                    chapter++;
                }
            }
        }

        private static void CopySrcToTemp(DirectoryInfo dirInfo)
        {
            foreach (var dir in dirInfo.GetDirectories())
            {
                CopySrcToTemp(dir);
            }

            foreach (var file in dirInfo.GetFiles("*.map"))
            {
                string guid = Guid.NewGuid().ToString();
                File.Copy(file.FullName, Path.Combine(tempFolder, guid + ".map"));
            }
        }
    }
}
